#!/usr/bin/python

from LogWriter import log
from globals import *

import random

DATAPATH = '../data/'


def randomColor():
    return [random.gauss(0.4, 1.0) for x in range (0,3)]


def loadModel(definition):
    model = None
    for operations in definition:
        nodePath = loader.loadModel(DATAPATH + operations[0])
        child = nodePath.getChild(0)
        for op in operations[1:]:
            cmd = op[0]
            args = op[1]
            
            if cmd == 'scale':
                x = 0.0
                if len(args) > 1:
                    x = args[0]
                    y = args[1]
                    if args[2] == 'random':
                        z = random.randint(2,6) / 2.0
                    else:
                        z = args[2]
                else:
                    x = args[0]
                    y = args[0]
                    z = args[0]

                child.setScale(x,y,z)
            elif cmd == 'translate':
                x = args[0]
                y = args[1]
                z = args[2]
                child.setPos(child, x, y, z)
            elif cmd == 'color':
                if args[0] == 'randomColor':
                    args = randomColor()
                
                r = args[0]
                g = args[1]
                b = args[2]
                child.setColor(r,g,b, 1.0)
            elif cmd == 'ground':
                height = child.getScale()[2]
                child.setZ(height / 2)
            else:
                log.write('error', "invalid operation name " + `cmd`)
                return None
        
        if model is None:
            # note : first object in definition will always be the root node
            model = nodePath
        else:
            nodePath.reparentTo(model)
    
    return model

joint = [ 
        ('cube', ('scale', (0.1,))),
]

robo_torso = [
        ('cube', 
            ('scale', (2.0,1.2,2.0)),
            ('color', ROBO_COLOR),
        ),
]
robo_leg = [
        ('cube', 
            ('translate', (   0,   0, -0.5)),
            ('scale',     ( 0.6, 0.6,  2)),
            ('color',     ROBO_COLOR),
        ),
]

robo_arm = [
        ('cube', 
            ('scale', (0.5,0.5,1.8)),
            ('translate', (0,0,-0.2)),
            ('color', ROBO_COLOR),
        ),
        ('cube', 
            ('scale', (0.2,0.4,0.6)),
            ('translate', (-1.2,0,-2.0)),
            ('color', ROBO_COLOR),
        ),
        ('cube', 
            ('scale', (0.2,0.4,0.6)), 
            ('translate', ( 1.2,0,-2.0)),
            ('color', ROBO_COLOR),
        ),
]

robo_head = [
        ('cube',
            ('scale', (1.8, 0.9, 1.5)),
            ('color', ROBO_COLOR),
        ),
        ('sphere',
            ('scale',     ( 0.2, 0.2, 0.2)),
            ('translate', (-2.5, 2.0, 1.0)),
            ('color',     ROBO_EYECOLOR),
        ),
        ('sphere',
            ('scale',     ( 0.2, 0.2, 0.2)),
            ('translate', ( 2.5, 2.0, 1.0)),
            ('color',     ( 0.8, 0.8,   0)),
        ),
]

lazor = [
    ('sphere',
        ('scale', (0.1,0.5,0.1)),
        ('color', ROBO_LAZORCOLOR),
    ),
]

building = [
    ('cube',
        ('scale', ( 1, 1, 'random' )),
        ('color', (0.7,0.7,0.7)),
        ('ground', []),
    ),
]

car = [
    # body
    ('cube',
        ('scale', (0.3, 0.6, 0.15)),
        ('color', ('randomColor',)),
        ('ground', []),
    ),
    # windows
    ('cube',
        ('translate', (0,0,0.2)),
        ('scale', (0.3, 0.3, 0.09)),
        ('color', (0.6,0.8,1.0)),
    ),
]

tank = [
    # body
    ('cube',
        ('scale', ( 0.4, 0.7, 0.2)),
        ('color', ( 0.3, 0.5, 0.2)),
        ('ground', []),
    ),
    # windows
    ('cube',
        ('translate', (0,0,0.3)),
        ('scale', (0.4, 0.4, 0.1)),
        ('color', (0.3, 0.5, 0.2)),
    ),
    ('cube',
        ('translate', (0,0.3,0.2)),
        ('scale', (0.1, 0.5, 0.1)),
        ('color', (0.3, 0.5, 0.2)),
    ),
]

soldier = [
    # body
    ('cube',
        ('scale', (0.1, 0.1, 0.2)),
        ('color', (0.3, 0.8, 0.2)),
        ('ground', []),
    ),
    # head
    ('cube',   
        ('translate', (0,0,0.25)),
        ('scale', (0.1, 0.1, 0.1)),
        ('color', (0.2, 0.8, 0.2)),
    ),
    # gewehr!
    ('cube',
        ('scale', (0.05, 0.3, 0.05)),
        ('translate', (0,0,0.2)),
        ('color', (0.4, 0.4, 0.4)),
    ),
]

human = [
    # body
    ('cube',
        ('scale', (0.1, 0.1, 0.2)),
        ('color', ('randomColor',)),
        ('ground', []),
    ),
    # head
    ('cube',
        ('translate', (0,0,0.25)),
        ('scale', (0.1, 0.1, 0.1)),
        ('color', (1.0, 0.9, 0.8)),
    ),
]

gameplane = [
    ('cube',
        ('translate', (0,0,-1)),
        ('color', (0.2,0.2,0.2)),
    ),
]
